package com.ruikong.dazhi.controller.basicinfo;

import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.basicinfo.DeviceType;
import com.ruikong.dazhi.model.output.TreeMenuOutput;
import com.ruikong.dazhi.service.basicinfo.IDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (DeviceType)表控制层
 *
 * @author HuaJiaYi
 * @since 2021-09-29 10:55:42
 */
@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "设备类型")
public class DeviceTypeController {
    private static final Logger logger = LoggerFactory.getLogger(DeviceTypeController.class);
    /**
     * 服务对象
     */
    @Autowired
    private IDeviceTypeService deviceTypeService;

    @PostMapping("/getDeviceTypeById")
    @ApiOperation("根据id获取设备类别信息")
    public BaseResult getDeviceTypeById(@RequestBody DeviceType s) {
        BaseResult result = new BaseResult();
        try {
            DeviceType stockType = deviceTypeService.queryById(s.getDeviceTypeId());
            if (stockType != null) {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.getSuccessMsg);
                result.setData(stockType);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.getFailMsg);
            }

        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("根据id获取设备类别信息出错：{}", e.getMessage());
        }
        return result;
    }

    @PostMapping("/editDeviceType")
    @ApiOperation("新增或修改设备类别信息")
    public BaseResult editDeviceType(@RequestBody DeviceType s) {
        BaseResult result = new BaseResult();
        try {
            if (s.getDeviceTypeId() != null) {
                if (deviceTypeService.isExist(s) > 0) {
                    result.setCode(ResultApi.failCode);
                    result.setMsg("该设备类别名称已经存在!");
                    return result;
                }
                int effect = deviceTypeService.update(s);
                if (effect > 0) {
                    result.setCode(ResultApi.successCode);
                    result.setMsg(ResultApi.updateSuccessMsg);
                } else {
                    result.setCode(ResultApi.failCode);
                    result.setMsg(ResultApi.updateFailMsg);
                }
            } else {
                if (deviceTypeService.isExist(s) > 0) {
                    result.setCode(ResultApi.failCode);
                    result.setMsg("该设备类别名称已经存在!");
                    return result;
                } else if (s.getDeviceTypeName() == "" || s.getDeviceTypeName() == null) {
                    result.setCode(ResultApi.failCode);
                    result.setMsg("请输入类别名称!");
                    return result;
                }
                s.setIsDeleted(0);
                int effect = deviceTypeService.insert(s);
                if (effect > 0) {
                    result.setCode(ResultApi.successCode);
                    result.setMsg(ResultApi.addSuccessMsg);
                } else {
                    result.setCode(ResultApi.failCode);
                    result.setMsg(ResultApi.addFailMsg);
                }
            }
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
            logger.error("编辑设备类别出错：{}", e.getMessage());
        }
        return result;
    }

    @PostMapping("/getDeviceTypeTree")
    @ApiOperation("绑定设备类别树")
    public BaseResult getDeviceTypeTree() {
        BaseResult result = new BaseResult();
        try {
            List<TreeMenuOutput> list = deviceTypeService.getDeviceTypeTree();
            if (list.size() == 0) {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.getFailMsg);
            } else {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.getSuccessMsg);
                result.setData(list);
            }
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("绑定设备类别树出错：{}", e.getMessage());
        }
        return result;
    }

    @PostMapping("/deleteDeviceType")
    @ApiOperation("删除设备类别")
    public BaseResult deleteDeviceType(@RequestBody DeviceType s) {
        BaseResult result = new BaseResult();
        try {
            String sReturn = deviceTypeService.deleteById(s.getDeviceTypeId());
            if (sReturn == "") {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.deleteSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(sReturn);
            }
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.deleteFailMsg);
            logger.error("删除设备类别出错：{}", e.getMessage());
        }
        return result;
    }
    
    
}
